﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.MQ
{
    /// <summary>
    /// 传输消息
    /// </summary>
    public class TransmitMessage
    {
        /// <summary>
        /// 消息的功能
        /// </summary>
        public string FunctionName
        {
            get;
            set;
        }

        /// <summary>
        /// 目的地Id
        /// </summary>
        public string DestinationId
        {
            get;
            set;
        }

        /// <summary>
        /// 消息内容
        /// </summary>
        public IPayLoad PayLoad
        {
            get;
            set;
        }

        /// <summary>
        /// 消息序列化
        /// </summary>
        /// <returns></returns>
        public string Serialize()
        {
            string message = FunctionName + ";" + PayLoad.Serialize();
            return message;
        }


        /// <summary>
        /// 消息反序列化
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public TransmitMessage Deserialize(string message, IPayLoad payload)
        {
            var transMessage = new TransmitMessage();
            var mes = message.Split(';');
            transMessage.PayLoad = payload.Deserialize(mes[1]);
            transMessage.FunctionName = mes[0];
            return transMessage;
        }
    }
}
